package tx.bj12320.service;

import org.activiti.engine.RuntimeService;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import javacommon.base.*;
import javacommon.util.*;

import cn.org.rapid_framework.util.*;
import cn.org.rapid_framework.web.util.*;
import cn.org.rapid_framework.page.*;
import cn.org.rapid_framework.page.impl.*;

import tx.bj12320.model.*;
import tx.bj12320.model.enums.DelayStateEnum;
import tx.bj12320.model.enums.SysUserTypeEnum;
import tx.bj12320.dao.*;
import tx.bj12320.service.*;
import tx.bj12320.vo.query.*;

@Service
@Transactional
public class TszbProcessInfoManager extends BaseManager<TszbProcessInfo,Long>{

	private TszbProcessInfoDao tszbProcessInfoDao;
	/**增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,请注意大小写*/
	public void setTszbProcessInfoDao(TszbProcessInfoDao dao) {
		this.tszbProcessInfoDao = dao;
	}
	public EntityDao getEntityDao() {
		return this.tszbProcessInfoDao;
	}

	@Transactional(readOnly=true)
	public Page findPage(TszbProcessInfoQuery query) {
		return tszbProcessInfoDao.findPage(query);
	}

	@Transactional(readOnly=true)
	public Page findPageWithUnReceive(TszbProcessInfoQuery query) {
		query.setIsReject(0L);//只查询未报废的工单
		return tszbProcessInfoDao.findPageWithUnReceive(query);
	}

	@Transactional(readOnly=true)
	public Page findPageWithUnDeal(TszbProcessInfoQuery query) {
		query.setIsReject(0L);//只查询未报废的工单
		return tszbProcessInfoDao.findPageWithUnDeal(query);
	}

	@Transactional(readOnly=true)
	public TszbProcessInfo getByProcInstId(String  procInstId) {
		return tszbProcessInfoDao.findByProperty("procInstId", procInstId);
	}

	private OrgInfoManager orgInfoManager;
	public void setOrgInfoManager(OrgInfoManager manager) {
		this.orgInfoManager = manager;
	}
	private RuntimeService runtimeService;
	public void setRuntimeService(RuntimeService runtimeService) {
		this.runtimeService = runtimeService;
	}

	/**
	 * 创建投诉转办流程
	 * @param tszbProcessInfo
	 * @return
	 */
	public boolean startProcess(TszbProcessInfo tszbProcessInfo){

		this.save(tszbProcessInfo);

		OrgInfo oi=orgInfoManager.getById(tszbProcessInfo.getOrgInfoId());
		Map<String, Object> vars = new HashMap<String, Object>();
		vars.put("tszbProcessInfoId", tszbProcessInfo.getTszbProcessInfoId());
		vars.put("orgInfoId", tszbProcessInfo.getOrgInfoId());
		vars.put("orgName", oi.getOrgName());
		vars.put("workOrderType", tszbProcessInfo.getWorkOrderType());

		ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("Tousu_zhuanban_process",tszbProcessInfo.getTszbProcessInfoId()+"",vars);

		tszbProcessInfo.setProcInstId(processInstance.getProcessInstanceId());
		tszbProcessInfo.setCurrActName(((ExecutionEntity)processInstance).getActivityId());
		tszbProcessInfo.setProcStartTime(new Date());
		tszbProcessInfo.setCurrSysUserType(SysUserTypeEnum.BJ12320.getCode());
		tszbProcessInfo.setIsProcEnd(new Long(0));
		tszbProcessInfo.setIsReject(new Long(0));
		tszbProcessInfo.setIsSecret(new Long(0));
		tszbProcessInfo.setDelayState(DelayStateEnum.UN_APPLY.getCode());

		this.saveOrUpdate(tszbProcessInfo);
		return true;
	}

}
